package com.googlecode.pdfbricks.cli.parser;

import java.io.File;

import org.apache.commons.cli.CommandLine;

import com.googlecode.pdfbricks.cli.constant.CommandConstant;
import com.googlecode.pdfbricks.cli.constant.OptionConstant;
import com.googlecode.pdfbricks.engine.base.Executable;
import com.googlecode.pdfbricks.splitter.SplittingBuilder;

public class SplitCommandParser implements CommandParser {

	private final static SplitCommandParser PARSER=new SplitCommandParser();
	
	private SplitCommandParser(){
		
	}
	
	public static SplitCommandParser getInstance(){
		return PARSER;
	}
	
	public Executable parse(CommandLine commandLine) {
		SplittingBuilder builder=null;
		if(commandLine==null){
			throw new NullPointerException("CommandLine is null");
		}else{
			builder=new SplittingBuilder();
		}
		
		if(commandLine.hasOption(OptionConstant.FROM.getOpt())){
			builder.from(new File(commandLine.getOptionValue(OptionConstant.FROM.getOpt())));
		}
		
		if(commandLine.hasOption(OptionConstant.TO.getOpt())){
			String toCommandStr=commandLine.getOptionValue(OptionConstant.TO.getOpt());
			String[] toFilePageRangeStrs=toCommandStr.split(CommandConstant.SEMICOLON);
			for(int i=0;i<toFilePageRangeStrs.length;i++){				
				builder.to(FilePageRangeParser.getInstance().parse(toFilePageRangeStrs[i]));
			}
		}
		
		return builder.build();
	}

}
